Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bicycle-codes/simple-aes

Package Overview
Dependencies
Maintainers
0
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bicycle-codes/simple-aes

An easy way to use symmetric keys in browsers or node

  • 0.0.27
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
19
decreased by-29.63%
Maintainers
0
Weekly downloads
 
Created
Source

simple AES

tests module types semantic versioning license

Cryptography used by vanishing.page. Works in browsers and node.

This is generally useful as a dead simple way of working with symmetric keys in a browser or node.

Thanks to Fission, the original author for much of this code.

install

npm i -S @bicycle-codes/simple-aes

use

bundler

Just import

import {
    decryptMessage,
    encryptMessage,
    type Message
} from '@bicycle-codes/simple-aes'
// } from '@bicycle-codes/simple-aes/compat'  // for older browsers

pre-bundled

This exposes pre-bundled & minified JS files.

copy
cp ./node_modules/@bicycle-codes/simple-aes/dist/compat.min.js ./public
cp ./node_modules/@bicycle-codes/simple-aes/dist/index.min.js ./public/simple-aes.min.js
<body>
    <!-- ... -->

    <script type="module" src="./compat.min.js"></script>
    <!-- or webcrypto version -->
    <script type="module" src="./simple-aes.min.js"></script>
</body>

API

@bicycle-codes/simple-aes

Use the webcrypto API. This will only work in newer browsers.

import {
    decryptMessage,
    encryptMessage,
    type Message
} from '@bicycle-codes/simple-aes'

@bicycle-codes/simple-aes/compat

Use a user-land module, @noble/ciphers. This will work in browsers of all ages.

import {
    encryptMessage,
    decryptMessage
} from '@bicycle-codes/simple-aes/compat'

encryptMessage

Generate a new AES key and encrypt the given message object. Return an array of [ encryptedMessage, { key }], where key is a new AES key, encoded as base64url.

async function encryptMessage (
    msg:{ content:string }
):Promise<[{ content:string }, { key }]>
encrypt example
import { encryptMessage } from '@bicycle-codes/simple-aes'

const [encryptedMsg, { key }] = await encryptMessage({
    content: 'hello world'
})

console.log(encryptedMessage)
// =>  { content: '5eAcA6+jnBfbuCx8L...' }

decryptMessage

Decrypt the given message with the given key. Suitable for decrypting a message that was encrypted by this library. Key is an AES key, base64url encoded.

async function decryptMessage (
    msg:{ content:string },
    keyString:string  // <-- base64url
):Promise<{ content:string }>
decrypt example
import { test } from '@bicycle-codes/tapzero'
import { decryptMessage } from '@bicycle-codes/simple-aes'

test('decrypt the message', async t => {
    const decrypted = await decryptMessage(message, key)
    t.equal(decrypted.content, 'hello world',
        'should decrypt to the right text')
})

Keywords

FAQs

Package last updated on 13 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc